﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using VOL.Entity.AttributeManager;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.EvaluateNurseCare;
using VOL.Entity.DomainModels.WorkforceManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.EvaluateNurseCare;
using VOL.SmartMecial.IServices.NurseManage;

namespace VOL.WebApi.Controllers.SmartMecial
{
    //[Route("api/[controller]")]
    //[ApiController]
    [Route("api/PatientReCordController")]
    [PermissionTable(Name = "PatientReCordController")]
    public class PatientReCordController : ControllerBase
    {
        private readonly IAddPatientReCordService _service;
        private readonly IPatientReCordService _patientReCordService;
        private readonly IPatientReCordShoowService _patientReCordShoowService;
        private readonly IPatientCodeService _patientCodeService;

        public PatientReCordController(IAddPatientReCordService service, IPatientReCordService patientReCordService, IPatientReCordShoowService patientReCordShoowService,IPatientCodeService patientCodeService)
        {
            this._service = service;
            this._patientReCordService = patientReCordService;
            this._patientReCordShoowService = patientReCordShoowService;
            this._patientCodeService = patientCodeService;
        }

        /// <summary>
        /// 添加护理记录
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpPost("AddPatientReCord")]
        public async Task<int> AddPatientReCord([FromBody] PatientReCordModel model)
        {
            return await _service.AddPatientReCord(model);
        }

        /// <summary>
        /// 床位护理记录
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpGet("GetPatientReCord")]
        public async Task<List<Patient>> GetPatientReCord()
        {
            return await    _patientReCordService.GetPatientReCord();
        }

        /// <summary>
        /// 显示护理记录
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        [HttpGet("ShowPatientReCord")]
        public async Task<List<PatientReCordDTO>> ShowPatientReCord()
        {
            return await _patientReCordShoowService.ShowPatientReCord();
        }


        /// <summary>
        /// 查询患者编号
        /// </summary>
        /// <param name="PatientCode">患者编号</param>
        /// <returns></returns>
        [HttpGet("GetPatientInfo")]
        public async Task<List<Patient>> GetPatientInfo(string PatientCode) 
        {

            return await  _patientCodeService.GetPatientInfo(PatientCode);
        }



        /// <summary>
        /// 电子签名上传到七牛云
        /// </summary>
        /// <param name="writeImage"></param>
        /// <returns></returns>
        [HttpPost("UpdateImg")]
        public IActionResult UpdateImg([FromBody] string writeImage)
        {
            string base64Signurl = writeImage?.Trim();
            if (string.IsNullOrEmpty(base64Signurl))
            {
                return Ok("signurl无效");
            }

            byte[] data;
            data = Convert.FromBase64String(base64Signurl);

            //请到官网申请七牛云账号、秘钥
            var accessKey = "OecJqF2vFd_TrnrYa-xCJWu6u6hbKSY_YbXtV96i";
            var secretKey = "DbFMydQXQ_qK9QNHSvyH4cRHOxrMPIShzQsuBZGk";

            //var accessKey = "T5dzsMPYmt64XX9a0cnrIZMiwYiN4EoBH-VufTSn";
            //var secretKey = "22v7FXhNtFJYxy8BSamVaDCiAIqnDxqiNeTB-MXh";

            Mac mac = new Mac(accessKey, secretKey);
            //保存到七牛云上的文件目录
            string saveKey = "MyImg/" + Guid.NewGuid() + ".jpg";
            // 存储空间名
            string Bucket = "battlefieldone";
            // 设置上传策略
            PutPolicy putPolicy = new PutPolicy();
            // 设置要上传的目标空间
            putPolicy.Scope = Bucket;

            // 如果需要设置为"覆盖"上传(如果云端已有同名文件则覆盖)，请使用 SCOPE = "BUCKET:KEY"
            // putPolicy.Scope = bucket + ":" + saveKey;

            // 上传策略的过期时间(单位:秒)
            putPolicy.SetExpires(3600);
            // 上传到云端多少天后自动删除该文件，如果不设置（即保持默认默认）则不删除
            putPolicy.DeleteAfterDays = 100;

            // 生成上传token
            string token = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

            Config config = new Config();
            // 设置上传区域  [CN_East:华东] [CN_South:华南] [CN_North:华北] [US_North:北美]
            config.Zone = Qiniu.Storage.Zone.ZONE_CN_North;
            // 设置 http 或者 https 上传
            config.UseHttps = true;
            config.UseCdnDomains = true;
            config.ChunkSize = ChunkUnit.U512K;

            // 数据流上传
            FormUploader fu = new FormUploader(config);
            //七牛云支持 stream流、MemoryStream流、Filestream流上传
            var result = fu.UploadData(data, saveKey, token, null);
            Console.WriteLine(result);

            var imgUrl = "http://svgpl38o2.hb-bkt.clouddn.com/" + saveKey;
            Console.WriteLine(imgUrl);
            return Ok(imgUrl);
        }

    }
}
